using System.IO;
using Volo.Abp.VirtualFileSystem;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Data;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.Dapper;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.EntityFrameworkCore.SqlServer;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.Studio;
using Volo.Abp.Uow;
using YaAppoint.DapperRepositories;

namespace YaAppoint.EntityFrameworkCore;

[DependsOn(
    typeof(YaAppointDomainModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpSettingManagementEntityFrameworkCoreModule),
    typeof(AbpEntityFrameworkCoreMySQLModule),
    typeof(AbpEntityFrameworkCoreSqlServerModule),
    typeof(AbpBackgroundJobsEntityFrameworkCoreModule),
    typeof(AbpAuditLoggingEntityFrameworkCoreModule),
    typeof(AbpFeatureManagementEntityFrameworkCoreModule),
    typeof(AbpIdentityEntityFrameworkCoreModule),
    typeof(AbpOpenIddictEntityFrameworkCoreModule),
    typeof(BlobStoringDatabaseEntityFrameworkCoreModule)
    )]
[DependsOn(typeof(AbpDapperModule))]
    public class YaAppointEntityFrameworkCoreModule : AbpModule
{

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        YaAppointEfCoreEntityExtensionMappings.Configure();
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddTransient<IDapperExamItemRepository, DapperExamItemRepository>();
        context.Services.AddTransient<IDapperExamApplyItemRepository, DapperExamApplyItemRepository>();

        context.Services.AddAbpDbContext<YaAppointDbContext>(options =>
        {
            options.AddDefaultRepositories(includeAllEntities: true); 
        });

        Configure<AbpDbContextOptions>(options => {
            options.PreConfigure<YaAppointDbContext>(opts => 
            {
                opts.DbContextOptions.UseLazyLoadingProxies();
            });
            var configuration = context.Services.BuildServiceProvider().GetService<IConfiguration>();
            if (configuration!["ConnectionStrings:DbType"] == "MySql")
            {
                options.UseMySQL();
            }
            else
            {
                options.UseSqlServer();
            }
            
        });

        Configure<AbpUnitOfWorkDefaultOptions>(options => {
            options.IsolationLevel = IsolationLevel.ReadCommitted;
        });

        if (AbpStudioAnalyzeHelper.IsInAnalyzeMode)
        {
            return;
        }
    }
}
